#include "stdafx.h"

#include "SkinMode.h"
#include "SkinUtil.h"

#include "tinyxml.h"

#include "SkinElement.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BOOL CSkinMode::LoadMode(TiXmlNode * pNode)
{
	CString strElementValue;

	strElementValue = CSkinElement::GetXMLElementValue(pNode, "name");

	if (strElementValue.IsEmpty())
		return FALSE;
	
	m_strModeName = strElementValue;
	
	for(pNode = pNode->FirstChild("element");pNode;pNode = pNode->NextSibling("element"))
	{
		MODE_ELEMENT modeElement;
		
		strElementValue = CSkinElement::GetXMLElementValue(pNode, "name");

		if (strElementValue.IsEmpty())
			return FALSE;		

		modeElement.strName = strElementValue;
		
		strElementValue = CSkinElement::GetXMLElementValue(pNode, "width_offset");
		if (!strElementValue.IsEmpty())
		{
			modeElement.m_sizeResizeOffset.cx = _ttoi( strElementValue );
		}
		else
		{
			strElementValue = CSkinElement::GetXMLElementValue(pNode, "width_to");
			if (!strElementValue.IsEmpty())
				modeElement.m_sizeSizeTo.cx = _ttoi( strElementValue );
			else 
			{
				strElementValue = CSkinElement::GetXMLElementValue(pNode, "width_prefer");
				if ( !strElementValue.IsEmpty() )
				{
					modeElement.m_bCXVaraible = ( strElementValue == _T("variable") );
					modeElement.m_bCXFixed = ( strElementValue == _T("fixed") );
				}
			}
		}

		strElementValue = CSkinElement::GetXMLElementValue(pNode, "height_offset");
		if (!strElementValue.IsEmpty())
			modeElement.m_sizeResizeOffset.cy = _ttoi( strElementValue );
		else
		{
			strElementValue = CSkinElement::GetXMLElementValue(pNode, "height_to");
			if (!strElementValue.IsEmpty())
				modeElement.m_sizeSizeTo.cy = _ttoi( strElementValue );
			else 
			{
				strElementValue = CSkinElement::GetXMLElementValue(pNode, "height_prefer");
				if ( !strElementValue.IsEmpty() )
				{
					modeElement.m_bCYVaraible = ( strElementValue == _T("variable") );
					modeElement.m_bCYFixed = ( strElementValue == _T("fixed") );
				}
			}
		}

		strElementValue = CSkinElement::GetXMLElementValue(pNode, "visible");
		if (!strElementValue.IsEmpty())
		{
			modeElement.m_bUpdateEnable_Visible = TRUE;
			modeElement.m_bVisible = (strElementValue == _T("true"));
		}

		strElementValue = CSkinElement::GetXMLElementValue(pNode, "src_image_move_offset");
		if( !strElementValue.IsEmpty() )
		{
			modeElement.m_sizeSrcImageMoveOffset.cx = 
				_ttoi( NateOnSkinUtil::GetIndexToken(strElementValue,_T(","),0) );
			modeElement.m_sizeSrcImageMoveOffset.cy = 
				_ttoi( NateOnSkinUtil::GetIndexToken(strElementValue, _T(","), 1) );
		}

		strElementValue = CSkinElement::GetXMLElementValue(pNode, "src_image_size_offset");
		if( !strElementValue.IsEmpty() )
		{
			modeElement.m_sizeSrcImageSizeOffset.cx = 
				_ttoi( NateOnSkinUtil::GetIndexToken(strElementValue, _T(","), 0) );
			modeElement.m_sizeSrcImageSizeOffset.cy = 
				_ttoi( NateOnSkinUtil::GetIndexToken(strElementValue, _T(","), 1) );
		}

		strElementValue = CSkinElement::GetXMLElementValue(pNode, "ownerdraw");
		if (!strElementValue.IsEmpty())
		{
			modeElement.m_bOwnerdraw = (strElementValue == _T("true"));
		}

		m_arrMODE_ELEMENT.Add(modeElement);
	}

	return TRUE;
}